Ext.define('AmpleHarvest.objects.cce.cCCE',
{
	extend				: 'Ext.tree.Panel',
	alias				: 'widget.ampleharvest-objects-cce-ccce',
	title				: 'CCE',
	collapsible			: true,
	border				: 0,
	iconCls				: 'toolbar-article-enhancement-cce',
	rootVisible			: false,
	arr_Highligh		:
	[
			"highlight_1", "highlight_2", "highlight_3", "highlight_4",
			"highlight_5", "highlight_6", "highlight_7", "highlight_8",
			"highlight_9", "highlight_10"
	],
	bubbleEvents	:
		[
		 	'highlight',
			'doclassificationchange'
		],
	listeners		:
	{
		brokerResponse	: function(obj_response)
		{
			this.add_JsonObject(obj_response.data);
			this.ahCaller.fireEvent('objectResponse', this);
			this.fireEvent(
					AmpleHarvest.layout.cLayout.C_STR_EVENT_CLASSIFICATION_CHANGE,
					obj_response.classification
			);
		}
	},

	initComponent		: function()
	{

		this.store = Ext.create('Ext.data.TreeStore',
		{
			root	:
			{
				expanded	: true,
				children	: []
			}
		});
		
		var util_db_dmetedata = Ext.create('AmpleHarvest.util.dataBroker');
		util_db_dmetedata.brokerData(this, this.m_str_id, 'ampleharvest-objects-cce-ccce');

		this.arr_Highligh.reverse();

		this.callParent();
		this.on("itemclick", function(obj_This, obj_Record, obj_Item,
						obj_Index, e, obj_Options)
				{

					if (!obj_Record.get('qtitle'))
					{
						return;
					}
					var str_Class = "";

					if (obj_Record.get('bool_Highlight'))
					{
						obj_Item.className = obj_Item.classNameOrig;
						obj_Record.set('bool_Highlight', false);
					}
					else
					{
						str_Class = this.get_HighlightClass();
						obj_Item.classNameOrig = obj_Item.className;
						obj_Item.className = obj_Item.className + " "
								+ str_Class;
						obj_Item.className = obj_Item.className.replace(
								"x-grid-row-focused", ""
						);
						obj_Item.className = obj_Item.className.replace(
								"x-grid-row-selected", ""
						);
						obj_Item.className = obj_Item.className.replace(
								"x-grid-row-over", ""
						);
						obj_Record.set('bool_Highlight', true);
					}

					obj_Record.set('cls', str_Class);
					this.fireEvent('highlight', obj_Record.get('qtitle'),
							str_Class, obj_Record.get('iconCls')
					);

				}, this);
	},

	get_HighlightClass	: function()
	{
		var str_Class = "";
		str_Class = this.arr_Highligh.pop();
		this.arr_Highligh.reverse();
		this.arr_Highligh.push(str_Class);
		this.arr_Highligh.reverse();
		return str_Class;
	},

	add_JsonObject		: function(JsonObject)
	{
		var win_display = '';
		this.itemCount = 0;
		if (JsonObject.CCEEntities)
		{
			this.ts_construct(JsonObject.CCEEntities, "entity_id", new Array(),
					this.getStore().getRootNode()
			);
			this.ts_construct(JsonObject.CCEValues, "value_id", new Array(),
					this.getStore().getRootNode()
			);
		}
	},

	ts_construct		: function(JsonObject, str_Attrib, storeArray,
			rootParent)
	{
		for (var value in JsonObject)
		{
			var partenNode = rootParent;
			var path = JsonObject[value].type.split('.');
			for (var i = 0; i <= path.length; i++)
			{
				if (i != path.length)
				{
					if (existNode = partenNode.findChild("text", path[i], true))
					{
						partenNode = existNode;
						continue;
					}
					partenNode = this.ts_add(partenNode, path[i], "group");
				}
				else
				{
					this.ts_add(partenNode, JsonObject[value].text, "leaf",
							JsonObject[value].id, str_Attrib
					);
					this.itemCount++;
				}
			}
		}
	},

	ts_add				: function(node_Obj, add_Obj, type, id, str_Attrib)
	{
		if (type == 'group')
		{
			var appended_node = node_Obj.appendChild(
					{
						text		: add_Obj,
						expanded	: true,
						children	: []
					});
			return appended_node;
		}
		else
		{// TODO : figure out why non-default variables are not accepted anymore
			node_Obj.appendChild(
					{
						qtitle			: id,
						text			: add_Obj,
						leaf			: true,
						iconCls			: str_Attrib
					});
		}
	},

	hasData				: function()
	{
		if (!this.itemCount)
		{
			return false;
		}
		if (this.itemCount < 1)
		{
			return false;
		}
		this.setTitle(this.title + ' (' + this.itemCount + ')');
		return true;
	}
}
);
